package com.shop.cereshop.app.controller.tencent.im;

import cn.hutool.core.util.StrUtil;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.enums.RedisEnum;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.service.im.ImUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("im")
@Api(value = "即时通讯模块", tags = "即时通讯模块")
@Slf4j
public class ImBaseController {
    @Resource
    private ImUserService imUserService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 获取鉴权
     */
    @ApiOperation(value = "获取鉴权")
    @RequestMapping(value = "getUploadSignature", method = RequestMethod.GET)
    public Result<String> getUploadSignature() {
        try {
            String uploadSignature = redisTemplate.opsForValue().get(RedisEnum.IM_SIGNATURE.getKey());
            if (StrUtil.isEmpty(uploadSignature)) {
                uploadSignature = imUserService.getUploadSignature();
//                redisTemplate.opsForValue().set(RedisEnum.IM_SIGNATURE.getKey());
                //设置缓存时间 1天
                redisTemplate.opsForValue().set(RedisEnum.IM_SIGNATURE.getKey(), uploadSignature, 1, java.util.concurrent.TimeUnit.DAYS);
            }
            return Result.success(uploadSignature);
        } catch (Exception e) {
            log.error("获取鉴权失败 {} ", e.getMessage());
            return Result.failed("获取鉴权失败");
        }
    }
}
